
com.sxnsx.xnaja.utilities = function () {
    var pub = {};
    var priv = {};
    var tag = "com.sxnsx.xnaja.utilities"; 
    this.$ = function (id) {
        return document.getElementById(id);
    };

    this.get_local_string = function (key) {
        var strbundle = this.$("xnaja_localization");
        var str = strbundle.getString(key);
        return str;
    };


    this.select_save_to_dir = function () {
        const nsIFilePicker = Components.interfaces.nsIFilePicker;
        var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
        fp.init(window, "", nsIFilePicker.modeGetFolder);
        var result = fp.show();
        if (result == nsIFilePicker.returnOK) {
            var file = fp.file;
            return file.path;
        } else {
            return '';
        }
    };

    this.match_site = function (url) {
        var downloaders = xnaja_common.downloaders;
        for (var i in downloaders) {
            var reg = new RegExp(downloaders[i][0]);
            if (url.match(reg) != null) {
                return i;
            }
        }
        return null;
    };

    this.download_prompt = function (url, method, start, end,  photo_size_lst) {
        var prompt_params = {accepted: 0,
                url_addr: url, page_from: start, page_to: end,
                photo_size: photo_size_lst,
                download_method: method,
                save_path: ""};

        window.openDialog("chrome://xnaja/content/prompt.xul", "",
            "chrome, dialog, modal", prompt_params).focus()
        if (prompt_params.accepted == 0) {
            return null;
        }
        return prompt_params;
    };

    this.get_url_param = function(url, param) {
        url += '&';
        var reg = new RegExp('.*'+ param +'=(.*?)&');
        return url.match(reg)[1];
    };

    this.get_url_part = function (url) {
        return url.split('//')[1].split('/');
    }

    this.xnaja_go = function (url) {
        var ioserv = Components.classes["@mozilla.org/network/io-service;1"]
            .getService(Components.interfaces.nsIIOService);
        var channel = ioserv.newChannel(url, 0, null);
        var stream = channel.open();

        if (channel instanceof Components.interfaces.nsIHttpChannel && channel.responseStatus != 200)
        {
            com.sxnsx.xnaja.ui.overlay.updata_status('download', 'Fail!');
            return "";
        }

        var bstream = Components.classes["@mozilla.org/binaryinputstream;1"]
            .createInstance(Components.interfaces.nsIBinaryInputStream);
        bstream.setInputStream(stream);

        var size = 0;
        var file_data = "";
        while(size = bstream.available()) {
            file_data += bstream.readBytes(size);
        }

        return file_data;
    };

    this.save_to_file = function (url, path) {
        // create a persist
        var persist = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"].
            createInstance(Components.interfaces.nsIWebBrowserPersist);

        var file = Components.classes["@mozilla.org/file/local;1"]
                .createInstance(Components.interfaces.nsILocalFile);
        file.initWithPath(path);

        var obj_URI = Components.classes["@mozilla.org/network/io-service;1"]
                .getService(Components.interfaces.nsIIOService)
                .newURI(url, null, null);

        // with persist flags if desired See nsIWebBrowserPersist page for more PERSIST_FLAGS.
        const nsIWBP = Components.interfaces.nsIWebBrowserPersist;
        const flags = nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
        persist.persistFlags = flags | nsIWBP.PERSIST_FLAGS_FROM_CACHE;

        // do the save
        persist.saveURI(obj_URI, null, null, null, "", file);
    }

    this.get_os = function () {
    	var os = Components.classes["@mozilla.org/xre/app-info;1"]
                .getService(Components.interfaces.nsIXULRuntime).OS;       
        return os;   
    }


};

